import 'package:flutter/services.dart';

class RuHunVerifyChannel {
  static const methodChannel = MethodChannel('ruhun_verify');

  static final _Callbacks _callback = _Callbacks(methodChannel);

  static Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  //初始化
  static Future<dynamic> initLoginWithId(
    String id,
    int timeout,
    String appName,
  ) async {
    final result = await methodChannel.invokeMethod<dynamic>(
      'initLoginWithId',
      {
        'id': id,
        'timeout': timeout,
        'appName': appName,
      },
    );
    return result;
  }

  //预登录
  static Future<dynamic> preLoginWithBack() async {
    final result = await methodChannel.invokeMethod<dynamic>('preLoginWithBack');
    return result;
  }

  //networkType 类型NSString
  //值 == 0.无网络; 1.数据流量; 2.wifi; 3.数据+wifi
  //carrier 类型NSNumber
  //值 == 0.未知(未插sim卡，其它运营商等); 1.中国移动mobile;2.中国联通unicom;3.中国电信telecom
  static Future<Map<dynamic, dynamic>?> getSimInfo() async {
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>?>('getSimInfo');
    return result;
  }

  static Future<bool?> closeAuthView() async {
    final result = await methodChannel.invokeMethod<bool>('closeAuthView');
    return result;
  }

  static Future<bool?> delectScrip() async {
    final result = await methodChannel.invokeMethod<bool>('delectScrip');
    return result;
  }

  //拉起授权页
  static Future<dynamic> loginWithController(String appName) async {
    final result = await methodChannel.invokeMethod<dynamic>('loginWithController', {"appName": appName});
    return result;
  }

  ///设置getToken回调
  static void getTokenCallback(Callback? callback) {
    _callback.tokenCallback = callback;
  }

  ///设置切换短信登录回调
  static void getSmsLoginCallback(Callback? callback) {
    _callback.smsLoginCallback = callback;
  }

  ///设置授权页面消失的回调
  static void getAuthViewDisposeCallback(Callback? callback) {
    _callback.authViewDisposeCallback = callback;
  }

  ///设置协议未勾选二次弹窗回调回调
  static void getUnCheckDialogCallback(Callback? callback) {
    _callback.unCheckDialogCallback = callback;
  }
}

///定义回调
typedef Callback = void Function(dynamic result);

class _Callbacks {
  Callback? tokenCallback;
  Callback? smsLoginCallback;
  Callback? authViewDisposeCallback;
  Callback? unCheckDialogCallback; //二次弹窗回调

  _Callbacks(MethodChannel channel) {
    channel.setMethodCallHandler((call) async {
      //拿到授权token
      if (call.method == "getToken") {
        var token = call.arguments;
        if (tokenCallback != null) {
          tokenCallback!(token);
        }
        return;
      }

      //切换短信登录
      if (call.method == "changeSmsLogin") {
        var arguments = call.arguments;
        if (smsLoginCallback != null) {
          smsLoginCallback!(arguments);
        }
        return;
      }

      //授权页面关闭
      if (call.method == "authViewDispose") {
        var arguments = call.arguments;
        if (authViewDisposeCallback != null) {
          authViewDisposeCallback!(arguments);
        }
        return;
      }

      //二次确认协议弹窗页面
      if (call.method == "unCheckDialog") {
        var arguments = call.arguments;
        if (unCheckDialogCallback != null) {
          unCheckDialogCallback!(arguments);
        }
        return;
      }
    });
  }
}
